package com.yuanmeng.auth.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import com.yuanmeng.common.core.constant.Constants;
import com.yuanmeng.common.core.constant.SecurityConstants;
import com.yuanmeng.common.core.domain.R;
import com.yuanmeng.common.core.enums.ApproveStatusEnum;
import com.yuanmeng.common.core.enums.ErrorCodeEnum;
import com.yuanmeng.common.core.enums.StatusEnum;
import com.yuanmeng.common.core.exception.ServiceException;
import com.yuanmeng.common.core.handler.ObjectValidateHandler;
import com.yuanmeng.common.core.handler.RemoteValidateHandler;
import com.yuanmeng.common.core.utils.StringUtils;
import com.yuanmeng.common.security.utils.SecurityUtils;
import com.yuanmeng.system.api.RemoteMerchantService;
import com.yuanmeng.system.api.RemoteUserService;
import com.yuanmeng.system.api.domain.MallMerchantEntity;
import com.yuanmeng.system.api.domain.response.MallMerchantResponse;
import com.yuanmeng.system.api.model.LoginUser;

/**
 * 商城商户登录校验方法
 *
 * @author 安然
 */
@Component
public class MerchantLoginService {

    @Autowired
    private SysLoginService       sysLoginService;

    @Autowired
    private RemoteMerchantService remoteMerchantService;

    @Autowired
    private RemoteUserService     remoteUserService;

    /**
     * 登录
     */
    public MallMerchantEntity MerchantLogin(String phoneNo, String password) {

        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(phoneNo, password)) {
            sysLoginService.recordLogininfor(phoneNo, Constants.LOGIN_FAIL, "商家手机号/密码必须填写");
            throw new ServiceException("商家手机号/密码必须填写");
        }

        // 查询商户信息
        R<MallMerchantResponse> merchantResult = remoteMerchantService.getMerchantByPhoneNo(phoneNo,
            SecurityConstants.INNER);

        if (R.FAIL == merchantResult.getCode()) {
            throw new ServiceException(merchantResult.getMsg());
        }

        if (ObjectUtils.isEmpty(merchantResult) || ObjectUtils.isEmpty(merchantResult.getData())) {
            sysLoginService.recordLogininfor(phoneNo, Constants.LOGIN_FAIL, "登录商户不存在");
            throw new ServiceException("登录用户：" + phoneNo + " 不存在");
        }
        MallMerchantEntity mallMerchantResEntity = merchantResult.getData().getMallMerchantEntity();

        if (StatusEnum.DISABLE.getCode().equals(mallMerchantResEntity.getStatus())) {
            sysLoginService.recordLogininfor(phoneNo, Constants.LOGIN_FAIL,
                "对不起，您的账号已冻结，请联系平台客服处理");
            throw new ServiceException("对不起，您的账号：" + phoneNo + " 已被冻结");
        }
        if (ApproveStatusEnum.FAIL.getCode().equals(mallMerchantResEntity.getApproveStatus())) {
            sysLoginService.recordLogininfor(phoneNo, Constants.LOGIN_FAIL,
                "对不起，您的账号申请审核失败，请修改后重新提交申请");
            throw new ServiceException("对不起，您的账号：" + phoneNo + " 已审核失败");
        }
        if (ApproveStatusEnum.APPROVING.getCode()
            .equals(mallMerchantResEntity.getApproveStatus())) {
            sysLoginService.recordLogininfor(phoneNo, Constants.LOGIN_FAIL,
                "对不起，您的账号申请正在审核中，请耐心等待审核通过后进行登录");
            throw new ServiceException("对不起，您的账号：" + phoneNo + " 申请正在审核中");
        }
        // 查询用户信息
        R<LoginUser> userResult = remoteUserService.getUserInfo(phoneNo, SecurityConstants.INNER);
        RemoteValidateHandler.remoteServiceFailHandler(userResult);

        ObjectValidateHandler.objectNotExistErrorHandler(userResult,
            ErrorCodeEnum.MERCHANT_INFO_ERROR);

        if (!SecurityUtils.matchesPassword(password, userResult.getData().getSysUser().getPassword())) {
            sysLoginService.recordLogininfor(phoneNo, Constants.LOGIN_FAIL, "用户密码错误");
            throw new ServiceException("用户不存在/密码错误");
        }

        sysLoginService.recordLogininfor(phoneNo, Constants.LOGIN_SUCCESS, "登录成功");
        return mallMerchantResEntity;
    }

}
